;; Tree-sitter setup

(require 'treesit)

(when (treesit-available-p)
  ;; 返回 t 则说明配置成功。但到这里安装并没有完成，这只是完成了核心部分，
  ;; 不同语言的解析器需要单独安装，不过幸好社区内已经有人整理好了，
  (setq treesit-font-lock-level 4
        ;; treesit-font-lock-level，这是配置高亮层级的，从低到高值可以是1到4
        ;; Emacs 默认从 treesit-extra-load-path 指定的路径去加载动态链接库，
        ;; 如果这个值是 nil它默认会从 user-emacs-directory 下的 tree-sitter 内加载。
        ;; treesit-extra-load-path "~/.emacs.d"
        )
  ;; 如果语言和动态链接库名字不对应，可以通过 treesit-load-name-override-list 来配置，比如：
  ;; (setq treesit-load-name-override-list
  ;;       '((c++ "libtree-sitter-cpp" "tree_sitter_cpp")
  ;;         (js "libtree-sitter-javascript" "tree_sitter_javascript")
  ;;         ))
  (setq treesit-language-source-alist
        '(
          ;; 以下列表中的内容移至语言对应的配置文件中，便于管理，此处保留用作参考
          ;; (c          . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-c"))
          ;; (cpp        . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-cpp"))

          ;; (javascript . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-javascript"))
          ;; (json       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-json"))
          ;; (typescript . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-typescript" nil "typescript/src"))
          ;; (tsx        . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-typescript" nil "tsx/src"))

          ;; (markdown   . ("https://github.moeyy.xyz/https://github.com/MDeiml/tree-sitter-markdown" nil "tree-sitter-markdown/src"))
          ;; (org        . ("https://github.moeyy.xyz/https://github.com/milisims/tree-sitter-org"))

          ;;(bash       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-bash"))
          ;;(css        . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-css"))
          ;;(cmake      . ("https://github.moeyy.xyz/https://github.com/uyha/tree-sitter-cmake"))
          ;;(csharp     . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-c-sharp.git"))
          ;;(dockerfile . ("https://github.moeyy.xyz/https://github.com/camdencheek/tree-sitter-dockerfile"))
          ;;(elisp      . ("https://github.moeyy.xyz/https://github.com/Wilfred/tree-sitter-elisp"))
          ;;(go         . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-go"))
          ;;(gomod      . ("https://github.moeyy.xyz/https://github.com/camdencheek/tree-sitter-go-mod.git"))
          ;;(html       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-html"))
          ;;(java       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-java.git"))
          ;;(lua        . ("https://github.moeyy.xyz/https://github.com/Azganoth/tree-sitter-lua"))
          ;;(make       . ("https://github.moeyy.xyz/https://github.com/alemuller/tree-sitter-make"))
          ;;(ocaml      . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-ocaml" nil "ocaml/src"))
          ;;(python     . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-python"))
          ;;(php        . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-php"))
          ;;(ruby       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-ruby"))
          ;;(rust       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-rust"))
          ;;(sql        . ("https://github.moeyy.xyz/https://github.com/m-novikov/tree-sitter-sql"))
          ;;(vue        . ("https://github.moeyy.xyz/https://github.com/merico-dev/tree-sitter-vue"))
          ;;(yaml       . ("https://github.moeyy.xyz/https://github.com/ikatyang/tree-sitter-yaml"))
          ;;(toml       . ("https://github.moeyy.xyz/https://github.com/tree-sitter/tree-sitter-toml"))
          ;;(zig        . ("https://github.moeyy.xyz/https://github.com/GrayJack/tree-sitter-zig"))
          ))

  ;; (dolist (source treesit-language-source-alist)
  ;;   (unless (treesit-ready-p (car source))
  ;;     (treesit-install-language-grammar (car source))))
  ;; 下面是另一种自动安装方法，不过没有上面的灵活
  ;;(mapc #'treesit-install-language-grammar
  ;; (mapcar #'car treesit-language-source-alist))

  ;; 内置类型重定向到 ts-mode;;好像不起作用
  ;; (custom-set-variables
  ;;  '(major-mode-remap-alist
  ;;    '((sh-mode       . bash-ts-mode)
  ;;      (js-mode       . js-ts-mode)
  ;;      (js-json-mode  . json-ts-mode)
  ;;      (css-mode      . css-ts-mode)
  ;;      (c-mode        . c-ts-mode)
  ;;      (c++-mode      . c++-ts-mode)
  ;;      (c-or-c++-mode . c-or-c++-ts-mode))))

  ;; 在安装完不同语言的动态链接库后，可以使用 (treesit-language-available-p 'c) 这个方式来测试。
  ;; (when (treesit-language-available-p 'elisp)
  ;;   (add-to-list 'major-mode-remap-alist '(elisp-mode . elisp-ts-mode)))
  ;; 非内置类型直接绑定
  ;; (add-to-list 'auto-mode-alist '("\\.zig\\'" . zig-ts-mode))
  ;; (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode))
  )

(provide 'init-treesit)
